#region imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

using BO = PHSRAG.Insight21.BusinessObjects;
using Utility = PHSRAG.Utility;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
using GenerateKey = PHSRAG.Insight21.BusinessObjects.ModuleKey;
#endregion

namespace PHSRAG.Insight21.Admin
{
	/// <summary>
	/// The Records business object contains a dataset that holds a collection of resource
	/// data that matches criteria specified in it's load method.
	/// </summary>
	public class Records : BO.BusinessObject
	{
		#region Constants
		private const string GetRecordTypeSPName			= "GetRecordTypeList";
		private const string RecordsTableName = "Records";
		private static string RecordsKey = GenerateKey.MakeKey("Records_CDF134CD-1BD2-43c1-9772-CA5EF31B456C",GenerateKey.Admin);
		#endregion

		#region Properties
		/// <summary>
		/// Get the key to be used in all caching operations for this instance of the object.
		/// </summary>
		public static string CacheKey	{ get { return RecordsKey; } }

		/// <summary>
		/// Gets the list of record types.  The stored Proc adds and item named "ALL" to the 
		/// resultset with and Id of 0.
		/// The data table returned is not cached because this property is called just once.
		/// </summary>
		public static DataTable RecordTypeTable
		{
			get
			{				
				return Utility.DataAccess.GetDataTable(Utility.DataAccess.GetConnectionString(), GetRecordTypeSPName);
			}
		}	
		#endregion

		#region Constructor
		/// <summary>
		/// Initialize the data containner.
		/// </summary>
		public Records(ICachePolicy cachePolicy) : base(cachePolicy)
		{
			DataContainer = new DataSet();
		}
		#endregion

		#region Base Methods
		public override void Cache()
		{
			CachePolicy.Cache(RecordsKey, this);
		}

		public override void UnCache()
		{
			CachePolicy.UnCache(RecordsKey);
		}

		/// <summary>
		/// Loads the data container with data from repository based on the RecordsCommandType
		/// passed in.  It is possible that no records match the passed in criteria.
		/// </summary>
		/// <param name="connectionString">The connection string to the data source.</param>
		/// <param name="args">arguments passed into the method.</param>
		public override void Load( string connectionString, params object[] args )
		{
			try
			{	
				Utility.DataAccess.LoadDataSet(DataContainer, RecordsTableName, connectionString, "GetRecordList",
                    Utility.DataAccess.BuildSqlParameterArray(
						"@domainUserId", SqlDbType.Int, args[0]
						,"@fundNumber", SqlDbType.VarChar, args[1]
						,"@folderNumber", SqlDbType.VarChar, args[2]
						,"@protocolNumber", SqlDbType.VarChar, args[3]
						,"@lastName", SqlDbType.VarChar, args[4]
						,"@firstName", SqlDbType.VarChar, args[5]
						,"@orgId", SqlDbType.Int, (int)args[6]
						,"@type", SqlDbType.Int, (int)args[7]
						,"@piDomainUserId", SqlDbType.Int, (int)args[8] 
						,"@keyGiverDomainUserId", SqlDbType.Int, (int)args[9]));			

				if ( !DataContainer.Tables.Contains( RecordsTableName) )
					throw new Exception("Failed to load record information.");

			}
			catch( Exception e)
			{
				Utility.ExceptionReport.WriteToEventLog ("Records", e, true, Utility.ExceptionReport.ReportType.Error);
				throw e;
			}
		}
		#endregion

	}
}
